package com.education.realtime.app.dws;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.education.realtime.app.BaseAppV2;
import com.education.realtime.bean.UserAppBean;
import com.education.realtime.bean.UserLouDou;
import com.education.realtime.common.Constant;
import com.education.realtime.util.AtguiguUtil;
import com.education.realtime.util.FlinkSinkUtil;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.ProcessAllWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;

import java.time.Duration;
import java.util.Map;

public class DwsUserAction extends BaseAppV2 {
    public static void main(String[] args) {
        new DwsUserAction().init(3007,
                "DwsUserAction",
                Constant.TOPIC_DWD_TRAFFIC_PAGE,
                Constant.TOPIC_DWD_TRADE_CART_ADD,
                Constant.TOPIC_DWD_PAY_ADD);
    }
    @Override
    protected void handle(StreamExecutionEnvironment env, Map<String, DataStreamSource<String>> stream) {
        DataStream<UserLouDou> unionStream = union(stream);

        SingleOutputStreamOperator<UserLouDou> reduceStream = windowReduce(unionStream);
//        reduceStream.print();

        reduceStream.addSink(FlinkSinkUtil.getClickHouseSink(Constant.CLICKHOUSE_DB,
                "dws_user_user_action_window", UserLouDou.class));
    }

    private SingleOutputStreamOperator<UserLouDou> windowReduce(DataStream<UserLouDou> unionStream) {
        return unionStream
                .assignTimestampsAndWatermarks(WatermarkStrategy.<UserLouDou>forBoundedOutOfOrderness(Duration.ofSeconds(3))
                        .withTimestampAssigner((obj, ts) -> obj.getTs()))
                .windowAll(TumblingEventTimeWindows.of(Time.seconds(5)))
                .reduce(new ReduceFunction<UserLouDou>() {
                            @Override
                            public UserLouDou reduce(UserLouDou value1, UserLouDou value2) throws Exception {
                                value1.setPageCt(value1.getPageCt() + value2.getPageCt());
                                value1.setDetailCt(value1.getDetailCt() + value2.getDetailCt());
                                value1.setCartCt(value1.getCartCt() + value2.getCartCt());
                                value1.setPayCt(value1.getPayCt() + value2.getPayCt());
                                return value1;
                            }
                        },
                        new ProcessAllWindowFunction<UserLouDou, UserLouDou, TimeWindow>() {
                            @Override
                            public void process(Context ctx, Iterable<UserLouDou> elements, Collector<UserLouDou> out) throws Exception {

                                String stt = AtguiguUtil.toDate(ctx.window().getStart(), "yyyy-MM-dd HH:mm:ss");
                                String edt = AtguiguUtil.toDate(ctx.window().getEnd(), "yyyy-MM-dd HH:mm:ss");
                                UserLouDou next = elements.iterator().next();
                                next.setEdt(edt);
                                next.setStt(stt);
                                out.collect(next);
                            }
                        });
    }

    private DataStream<UserLouDou> union(Map<String, DataStreamSource<String>> stream) {
        SingleOutputStreamOperator<UserLouDou> pageStream = stream.get(Constant.TOPIC_DWD_TRAFFIC_PAGE)
                .map(new MapFunction<String, UserLouDou>() {
                    @Override
                    public UserLouDou map(String value) throws Exception {
                        JSONObject obj = JSON.parseObject(value);
                        JSONObject page = obj.getJSONObject("page");
                        Long pageCt = 0L;
                        Long detailCt = 0L;
                        Long payCt = 0L;
                        Long cartCt = 0L;
                        Long ts = obj.getLong("ts");
                        if (page.getString("page_id").equals("home")) {
                            pageCt += 1;
                        } else if (page.getString("page_id").equals("course_detail")) {
                            detailCt += 1;
                        }
                        return new UserLouDou("", "", pageCt, detailCt, cartCt, payCt, ts);
                    }
                });
        SingleOutputStreamOperator<UserLouDou> cartStream = stream.get(Constant.TOPIC_DWD_TRADE_CART_ADD)
                .map(new MapFunction<String, UserLouDou>() {
                    @Override
                    public UserLouDou map(String value) throws Exception {
                        JSONObject obj = JSON.parseObject(value);
                        JSONObject page = obj.getJSONObject("page");
                        Long pageCt = 0L;
                        Long detailCt = 0L;
                        Long payCt = 0L;
                        Long cartCt = 1L;
                        Long ts = obj.getLong("ts");
                        return new UserLouDou("", "", pageCt, detailCt, cartCt, payCt, ts*1000);
                    }
                });

        SingleOutputStreamOperator<UserLouDou> payStream = stream.get(Constant.TOPIC_DWD_TRADE_CART_ADD)
                .map(new MapFunction<String, UserLouDou>() {
                    @Override
                    public UserLouDou map(String value) throws Exception {
                        JSONObject obj = JSON.parseObject(value);
                        JSONObject page = obj.getJSONObject("page");
                        Long pageCt = 0L;
                        Long detailCt = 0L;
                        Long payCt = 1L;
                        Long cartCt = 0L;
                        Long ts = obj.getLong("ts");
                        return new UserLouDou("", "", pageCt, detailCt, cartCt, payCt, ts*1000);
                    }
                });
        return pageStream.union(cartStream,payStream);
    }
}
